<?php
$templateId = Yii::app()->request->getParam('id');
$caption = empty($templateId) ? BUtils::T('template_create_lbl') : BUtils::T('template_modify_lbl');
$this->pageTitle = $caption . ' | ' . Yii::app()->user->name;

//get offer image
$url_picture = BUtils::GetOfferPictureUrl(str_replace('thumb_', '', $model->url_picture));

//discount
$discount = BUtils::GetDiscount($model->value, $model->price);
$discount_string = empty($discount) ? '' : sprintf('-%d', $discount) . '%';
?>
<div class="container prepend-top">
	<?php echo CHtml::button('< ' . BUtils::T('back_btn'), array('class'=>'button', 'onclick'=>"history.go(-1)")); ?>
</div>

<div class="container prepend-top append-bottom relative">
	<div class="span-15">
		<div class="box_white">
		<div class="biz-form">
			<div class="margin">
				<h3><?php echo $caption; ?></h3>
				<div class="tips"><?php echo BUtils::T('template_offer_eg'); ?></div>
			</div>
<?php
$form = $this->beginWidget('CActiveForm', array(
	'id'=>'template-form',
	'enableClientValidation'=>true,
	'clientOptions'=>array(
		'validateOnSubmit'=>true,
	),
));
//set the hidden input
echo $form->hiddenField($model, 'url_picture');
?>
<div class="fieldswrap">
<div class="fields">
	<div class="block">
<?php $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
	'id' => 'uploadOfferImage',
	'config' => array(
		'buttonName' => BUtils::T('upload_new_picture_eg'),
		'action' => Yii::app()->createUrl('/special/uploadOfferImage?id='. $model->id),
		'allowedExtensions'=>array("jpg","jpeg","gif","png"),
		'sizeLimit'=>8*1024*1024,
		'onComplete' => "js:function(id, fileName, responseJSON){ handleUploadedOffer(responseJSON); }",
	),
));
?>
	</div>
</div>
<div class="fields">
	<div class="block">
		<div class="lbl"><?php echo $form->labelEx($model,'content'); ?></div>
		<div class="ipt"><?php echo $form->textArea($model,'content', array('class'=>'auto', 'rows'=>'5')); ?></div>
		<div class="eg"><?php echo BUtils::T('special_content_eg'); ?></div>
		<div class="spc"><?php echo $form->error($model,'content'); ?></div>
	</div>
</div>
<div class="fields">
	<div class="block">
		<div class="span-5">
			<div class="lbl"><?php echo $form->labelEx($model,'value'); ?></div>
			<div class="ipt">
				<span class="symbol">€</span>
				<span class="tidy">
					<?php echo $form->textField($model, 'value', array('maxlength'=>10, 'size'=>10, 'class'=>'cal_amount')); ?>
				</span>
				<div class="clear"></div>
				<div style="margin-left:20px;"><?php echo $form->error($model,'value'); ?></div>
			</div>
		</div>
		<div class="span-5">
			<div class="lbl"><?php echo $form->labelEx($model,'price'); ?></div>
			<div class="ipt">
				<span class="symbol">€</span>
				<span class="tidy">
					<?php echo $form->textField($model, 'price', array('maxlength'=>10, 'size'=>10, 'class'=>'cal_amount')); ?>
				</span>
				<div class="clear"></div>
				<div style="margin-left:20px;"><?php echo $form->error($model,'price'); ?></div>
			</div>
		</div>
		<div class="span-2 last">
			<div class="lbl"><?php echo $form->labelEx($model,'discount') ?></div>
			<div class="ipt">
				<span class="special-discount" id="special-discount"><?php echo $discount_string; ?></span>
			</div>
			<div class="clear"></div>
		</div>
		<div class="clear"></div>
		<div style="margin-left:20px;"><?php echo $form->error($model,'discount'); ?></div>
		<div class="spc"></div>
	</div>
</div>
</div><!-- end of .fieldswrap -->

	<div class="margin actions">
		<?php echo CHtml::submitButton(BUtils::T('save_btn'), array('class' => 'button button-huge button-confirm')); ?>
		<?php echo CHtml::button(BUtils::T('cancel_btn'), array('class'=>'button button-huge button-thin', 'onclick'=>'history.go(-1);', 'confirm'=>BUtils::T('cancel_btn_msg'))); ?>
	</div>

<?php $this->endWidget(); ?>
			
		</div><!-- end of <div class="biz-form"> -->
		</div>
	</div>
	
	<div class="span-9 right last" id="sticky-right">
		<div class="box_white">
			<div class="biz-form margin">
				<h3><?php echo BUtils::T('preview_special_lbl'); ?></h3>
				<div class="eg"><?php echo BUtils::T('last_update_eg'); ?>: <span class="update-time"><?php echo $model->access_time; ?></span></div>
				<div class="prepend-top append-bottom">
<div class="offer_v">
	<div class="image relative">
		<img id="img_offer" src="<?php echo $url_picture; ?>" />
		<div class="clearfix info">
			<div class="period">
				<div class="hour-range" id="hour-range">Valid time</div>
				<div class="valid-date" id="valid-date"><?php echo BUtils::T('today_lbl'); ?></div>
			</div>
			<div class="discount">
				<span class="special-discount"><?php echo $discount_string; ?></span>
			</div>
		</div>
	</div>
	<div class="detail">
		<div class="remaining"></div>
		<div class="margin">
			<div class="title"><?php echo BUtils::T('special_price_lbl'); ?></div>
			<div class="clearfix text">
				<div class="left price">€ <span id="price"><?php echo $model->price; ?></span></div>
				<div class="left value"><span class="oldprice space-1" id="value">€<?php echo $model->value; ?></span></div>
			</div>
		</div>
		<div class="margin">
			<div class="title"><?php echo BUtils::T('special_lbl'); ?></div>
			<div class="text">
				<span id="special-content"><?php echo nl2br($model->content); ?></span>
			</div>
		</div>
	</div>
</div>
				</div>
			</div>
		</div>
	</div>
	
</div>
<script type="text/javascript">
$(function() {
	//set the preview box as sticky style.
	$('#sticky-right').stickyfloat({duration:0});
	
	//real-time view: special content
	if ($('#SpecialTemplateForm_content').val() == "") {
		$('#special-content').hide();
	}
	$('#SpecialTemplateForm_content').keyup(function (event) {
		if ($('#SpecialTemplateForm_content').val() == "") {
			$('#special-content').hide();
		} else {
			$('#special-content').show();
		}
		$('#special-content').html(nl2br($(this).val()));
	});
	//real-time to calculate discount
	$('.cal_amount').keyup(function() {
		var _price = get_price('#SpecialTemplateForm_price');
		var _value = get_price('#SpecialTemplateForm_value');
		set_discount(_price, _value);
	});
	//submit action
	$('#template-form').submit(function(){
		var _price = get_price('#SpecialTemplateForm_price');
		var _value = get_price('#SpecialTemplateForm_value');
		var _discount = set_discount(_price, _value);
		if (isNaN(_discount)) {
			return false;
		} else if (_discount > -20) {
			return false;
		}
	});
});

//
// amount = get_price('#coupon_price')
function get_price(id_prefix) {
	return parseFloat($(id_prefix).val());
}
//
// discount = set_discount(10, 20);
// -50
function set_discount(price, value) {
	if (isNaN(value) || isNaN(price)) {
		return NaN;
	}
	
	var _discount = 100;
	if (value > 0) {
		_discount = Math.round(price/value * 100.0 - 100.0);
		//give feedback
		$('.special-discount').html(_discount + '%');
		$('#SpecialForm_show_price').val('1');
		if (_discount > -20) {
			$('.special-discount').html(_discount + '%').attr('style', 'color:red');
			$('#SpecialTemplateForm_discount_em_').html('<?php echo BUtils::T('special_price_discount_msg'); ?>').show();
			return _discount;
		} else {
			$('.special-discount').html(_discount + '%').attr('style', '');
			$('#SpecialTemplateForm_discount_em_').html('').hide();
		}
		$('#price-section').fadeIn();
	} else {
		console.log(price, value);
		$('.special-discount').html('');
	}
	return _discount;
}

function handleUploadedOffer(respJSON) {
	if (respJSON.error) {
		$('.qq-upload-button').html('<span>'+respJSON.error+'</span>');
	} else {
		if (respJSON.url_offer_img) {
			$('#img_offer').attr('src', respJSON.url_offer_img);
			$('#SpecialTemplateForm_url_picture').val(respJSON.url_offer_img_db);
			$('ul.qq-upload-list').delay(1000).html('');
		}
	}
}
</script>